﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using VirtuOS.Base;
using System.Data;
using System.IO;

namespace VirtuOS.Components.Commands
{
    class calc
    {
        public static string help = "Evaluates simple math equations.";
        public static string register_name = "calc";
        public static bool requires_root = false;

        public static void Command(string[] args)
        {
            if ((args.Length > 0) && (Regex.Match(args[0], @"(-)?-help", RegexOptions.IgnoreCase).Success))
            {
                Console.WriteLine(help);
            }
            else
            {
                string eval = string.Empty;
                for (int i = 0; i < args.Length; i++)
                {
                    eval = eval + " " + args[i];
                }
                eval = eval.Trim();
                try
                {
                    eval = Evaluate(eval).ToString();
                    Console.WriteLine(eval);
                }
                catch (SyntaxErrorException e)
                {
                    Console.WriteLine(e);
                }
            }
        }

        private static double Evaluate(string expression)
        {
            var loDataTable = new DataTable();
            var loDataColumn = new DataColumn("Eval", typeof(double), expression);
            loDataTable.Columns.Add(loDataColumn);
            loDataTable.Rows.Add(0);
            return (double)(loDataTable.Rows[0]["Eval"]);
        }
    }
}
